CREATE FUNCTION authenticate(username character varying, pwdhash character varying)
  RETURNS boolean AS
$BODY$
DECLARE
	return_val smallint := -19;

	parool character varying;

	vastete_arv smallint;
	
BEGIN

	 SELECT count(*) into vastete_arv FROM kasutaja k where k.konto_nimi = username;

	if vastete_arv != 1 then
		return false;
	end if;

	SELECT k.parool into parool FROM kasutaja k where k.konto_nimi = username;

	if parool = pwdhash then
		return true;
	else
		return false;
	end if;
	
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION authenticate(character varying, character varying)
  OWNER TO meeskonnatoo;